Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
"Object is possibly undefined" when using Object.keys()

I'm having an issue with this piece of code, and assuring TypeScript that I know the location won't be undefined (as I'm iterating the People?).

On each line inside PeopleCard I'm getting Object is possibly 'undefined'.

I'm a little confused as if the Object is being iterated, I'm assuming it can't iterate

          {people && Object.keys(people).map((id: string) => (
            <PeopleCard
              location={people[id]}
              onDelete={() => openDeletePeopleModal(locations[id])}
              onEdit={() => openEditPeopleDrawer(locations[id])}
              onClick={() => history.push(`/todo/${todo.id}/people/${people[id].id}`)}
            />
          ))}

I get people from my Context, and I access it like so:

let { people } = selectedTodo;

TypeScript believes selectedTodo to be of type ITodo and people to be of type { [id: string]: Location } | undefined. Which seems correct to me.

My types are like so:

export type People = {
  id: string;
  peopleName: string;
  address1: string;
  address2?: string;
  address3?: string;
  address4?: string;
  address5?: string;
  town: string;
  postcode: string;
  country: string;
}

export type ITodo = {
  id: string;
  todoName: string;
  todoType: string;
  people?: { [id: string]: People };
};

I thought adding another check for undefined in might seal the deal, but whatever I've tried so far hasn't worked. I can just add ! after each one, but I'd rather not do that if it's easy to fix.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda